﻿using System;
using System.Text;

namespace ProgramUpDate
{
    public class stringBuilder
    {
        public const string sep = "'";
        public const string douhao = ", ";
        public const string first = "( ";
        public const string kuohao = " ) ";
        public const string semicolon = "; ";

        public StringBuilder result;
        public stringBuilder(string msg = "")
        {
            result = new StringBuilder(msg);
        }
        public stringBuilder appendFirst(object data)
        {
            this.result.Append(addSep(data, 1));
            return this;
        }

        public stringBuilder append(object data)
        {
            this.result.Append(addSep(data, 2));
            return this;
        }

        public stringBuilder appendLast(object data)
        {
            this.result.Append(addSep(data, 3));
            return this;
        }

        public stringBuilder appendFunc(object data)//1 ORCALE ;2 SQL
        {
            this.result.Append(addSep(data, 4));
            return this;
        }
        public stringBuilder appendSemicolon()//1 ORCALE ;2 SQL
        {
            this.result.Append(addSep("", 5));
            return this;
        }
        public stringBuilder appendSelf(object data)
        {
            this.result.Append(addSep(data, 6));
            return this;
        }
        public stringBuilder appendEnd()
        {
            this.result.Append(addSep("", 7));
            return this;
        }
        public static object addSep(object data, int last)
        {
            switch (last)
            {
                case 1:
                    return first + addSep(data) + douhao;
                case 2:
                    return addSep(data) + douhao;
                case 3:
                    return addSep(data) + kuohao;
                case 4:
                    return data + douhao;
                case 5:
                    return semicolon;
                case 6:
                    return data;
                case 7:
                    return kuohao;
            }
            return addSep(data) + douhao;
        }
        public static object addSep(object data)
        {
            if (data == null || string.IsNullOrEmpty(data.ToString()))
            {
                return sep + " " + sep;//orcale  不能放入''
            }

            if (data.GetType() == typeof(Char))
            {
                char temp = (char)data;
                if (temp == Char.MinValue)
                {
                    return sep + " " + sep;
                }
            }

            if (data.GetType() == typeof(DateTime))
            {
                DateTime temp = DateTime.Parse(data.ToString());
                if (temp == DateTime.MinValue)
                {
                    return " (SELECT sysdate  FROM DUAL) ";//当前时间
                }
                return string.Format("to_date('{0}', '{1}')", data.ToString(), "yyyy-MM-dd HH24:mi:ss");//空值占位符
            }
            return sep + data + sep;
        }
        override
        public string ToString()
        {
            return this.result.ToString();
        }
    }
}